package com.specialtreasure.freightdepot.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.core.activity.BaseNewActivity;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.util.UserUtils;
import com.app.core.view.MarqueeTextView;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.adapter.MyOrderAdapter;
import com.specialtreasure.freightdepot.model.BaseTbItemOrder;
import com.specialtreasure.freightdepot.model.TbItemOrder;
import com.specialtreasure.freightdepot.model.TbItemOrderData;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/21 0021.
 * 我的订单
 */

public class MyOrderActivity extends BaseNewActivity implements View.OnClickListener {
    public static final String TAG = "MyOrderActivity";
    Integer count = 10;//每页条数
    Integer page = 1;//页数
    String token;
    List<TbItemOrder> tbItemOrders=new ArrayList<TbItemOrder>();
    Boolean hasMode;
    MyOrderAdapter myOrderAdapter;


    private ImageView iv_back;
    private TextView tv_title;
    private MarqueeTextView tv_navigation;
    private ListView lv_moto_cade;
    private PtrClassicFrameLayout ptr_layout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_order);
        initView();
        setData();
        getAD();

    }

    private void setData() {
        show(iv_back);
        tv_title.setText("我的订单");


        //初始化自动刷新
        ptr_layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_layout.autoRefresh(true);
            }
        }, 150);

        //下拉刷新
        ptr_layout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr_layout.setLoadMoreEnable(true);
                page=1;
                tbItemOrders.clear();
                getData();

            }
        });

        //自动加载更多
        ptr_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (!hasMode){
                    page++;
                    getData();
                }else {
                    Toast.makeText(mContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
                    ptr_layout.setLoadMoreEnable(false);
                }
            }
        });

    }


    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_navigation = (MarqueeTextView) findViewById(R.id.tv_navigation);
        lv_moto_cade = (ListView) findViewById(R.id.lv_moto_cade);
        ptr_layout = (PtrClassicFrameLayout) findViewById(R.id.ptr_layout);

        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 网络请求
     */
    private void getData() {
        token= UserUtils.getToken();
        String url = Constants.HTTP_URL + HttpApi.MYORDERLIST;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", token);
        request.add("count", count);
        request.add("page", page);

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                ptr_layout.refreshComplete();
                ptr_layout.loadMoreComplete(true);

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);

                BaseTbItemOrder model = JSON.parseObject(result,BaseTbItemOrder.class);

                if (model != null) {
                    Integer status = model.getStatus();
                    String msg = model.getMsg();
                    //判断状态是
                    if (status != null && status == 0) {
                        TbItemOrderData data=model.getData();
                        if (data!=null){
                            hasMode=data.getHasMore();
                            //适配数据
                            List<TbItemOrder> tempList=data.getResultList();
                            if (tempList!=null&&tempList.size()>0){
                                tbItemOrders.addAll(tempList);
                                ptr_layout.setLoadMoreEnable(true);
                            }else {
                                ptr_layout.setLoadMoreEnable(false);
                            }

                            if (myOrderAdapter==null){
                                myOrderAdapter=new MyOrderAdapter(MyOrderActivity.this);
                                myOrderAdapter.setTbItemOrders(tbItemOrders);
                                lv_moto_cade.setAdapter(myOrderAdapter);
                            }else {
                                myOrderAdapter.setTbItemOrders(tbItemOrders);
                                myOrderAdapter.notifyDataSetChanged();
                            }
                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });
    }

    /**
     * 获取广告
     */
    private void getAD(){
        String url = Constants.HTTP_URL + HttpApi.ADVERTLIST;
        StringRequest request = new StringRequest(url, RequestMethod.POST);

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ONE, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                closeDialog();

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);

                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject != null) {
                    Integer status = jsonObject.getInteger("status");
                    String msg = jsonObject.getString("msg");

                    if (status != null && status == 0) {
                        List<String> data = JSON.parseArray(jsonObject.getJSONArray("data").toString(), String.class);
                        if (data != null&& data.size()>0) {
                            tv_navigation.init(data);
                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();

                    }

                }

            }


        });
    }


    @Override
    public String getActiviyName() {
        return TAG;
    }
}
